﻿using UnityEngine;
using System.Collections;

namespace KaoticSSB.Online
{
	public class NetworkManager : MonoBehaviour 
	{
		public static int NumPlayers = 32;
		public static int Port = 25000;

		public NetworkView GetNetworkView() { return mNetworkView; }
		public static bool Connected
		{
			get;
			set;
		}

		// Use this for initialization
		void Start () 
		{
			if(mNetworkView == null)
			{
				mNetworkView = GameObject.FindObjectOfType<NetworkView>();
			}
		}
		// Update is called once per frame
		void Update () 
		{
		
		}

		public static bool StartServer()
		{
			NetworkConnectionError error = Network.InitializeServer(NumPlayers, Port, true);
			bool success = (error == NetworkConnectionError.NoError);
			if(!success)
			{
				print (error);
			}
			else
			{
				print ("Server Initialized");
				MasterServer.RegisterHost("KaoticSSB", "KaoticSSB", "Desc.");
				print ("Registered to master server");
			}
			return success;
		}

		public static bool ConnectToServer(string address, int port, string password)
		{
			bool connected = false;
			NetworkConnectionError error = Network.Connect (address, port, password); 
			connected = (error == NetworkConnectionError.NoError);
			if(!connected)
			{
				print (error);
			}
			else
			{
				print ("Connected to server");
			}
			return connected;
		}

		private bool mConnectedToServer;
		private NetworkView mNetworkView;
	}
}